// pages/contractdetails/contractdetails.js
var app = getApp();
var com = require('../../common.js');
var util = require('../../utils/util.js')
function getDates(days, todate) {
  var dateArry = [];
  for (var i = 0; i < days; i++) {
    var dateObj = dateLater(todate, i);
    dateArry.push(dateObj)
  }
  return dateArry;
}
function dateLater(dates, later) {
  let dateObj = {};
  let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  let date = new Date(dates);
  date.setDate(date.getDate() + later);
  let day = date.getDay();
  let yearDate = date.getFullYear();
  let month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
  let dayFormate = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  dateObj.time = yearDate + '-' + month + '-' + dayFormate;
  dateObj.week = show_day[day];
  return dateObj;
}
Page({
  data: {
    height: 0,
    supplier: '',
    storeInfo: {},
    timelist:''
  },
  onLoad: function(options) {
    var _this = this,
     timess = util.formatDate(new Date()),
     dates = getDates(365, timess)
    console.log(dates[364].time);
    _this.supplier()
    _this.setData({
      timelist: dates[364].time,
      storeInfo:JSON.parse(options.info),
    })
    var time = util.formatTime(new Date()) + 365;
    console.log(time.substring(0,10))
    var times = time.substring(0, 4) + '-' + time.substring(5, 7) + '-' + time.substring(8, 10)
    console.log(times)
    _this.setData({
      time: times,
      phone: wx.getStorageSync('phone')
    })
  },
  showimg(e){
    var img = e.currentTarget.id
    wx.previewImage({
      urls: [img],
    })
  },
  //获取合同内容
  supplier() {
    var _this = this,
      urls = app.link.appraisal,
      data = {}
    com.link(urls, data, function(rep) {
      console.log(rep)
      var url = app.link.supplier + rep.data.response.response.supplier + '.st'
      com.link(url, {}, function(res) {
        console.log(res.data.response)
        wx.getImageInfo({
          src: res.data.response.contract.contract,
          success(e) {
            console.log(e)
            _this.setData({
              info: res.data.response,
              height: e.height,
              supplier: rep.data.response.response.supplier
            })
          }
        })

      }, 'GET')
    }, 'GET')

  },
  sign: function() {
    var _this = this,
      i = _this.data.storeInfo
    // if (!i.account_name) {
    //   wx.showToast({
    //     title: '请填写开户名称',
    //     icon:'none'
    //   })
    //   return false;
    // } else if (!i.account_bank) {
    //   wx.showToast({
    //     title: '请填写开户银行',
    //     icon:'none'
    //   })
    //   return false;
    // } else if (!i.account_number) {
    //   wx.showToast({
    //     title: '请填写银行账号',
    //     icon:'none'
    //   })
    //   return false;
    // }
    wx.showModal({
      title: '确认信息',
      content: '确定签署该合同吗？',
      confirmText: '确认签署',
      confirmColor: '#1b8c80',
      cancelColor: "#999999",
      success: res => {
        if (res.confirm) {
          let url = app.link.signingsupplier + this.data.supplier + '.st'
          com.link(url, i, function(rep) {
            console.log(rep)
            if (rep.data.state) {
              com.toast('签署成功')
              wx.removeStorage({
                key: 'functionId'
              })
              setTimeout(() => {
                wx.reLaunch({
                  url: '/pages/index/index',
                })
              }, 1500)
            } else {
              com.toast(rep.data.message)
            }
          }, "GET")
        }
      }
    })
  },
  showListindex() {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  onBindInput(e) {
    var _this = this,
      info = _this.data.storeInfo,
      id = e.currentTarget.id
    if (id == 1) {
      info.account_name = e.detail.value
    } else if (id == 2) {
      info.account_bank = e.detail.value
    } else if (id == 3) {
      info.account_number = e.detail.value
    }
    _this.setData({
      storeInfo: info
    })
  },
  onReady: function() {},
  onShow: function() {},
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  onShareAppMessage: function() {}
})